package com.sctlqq.wuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.Goods;
import com.sctlqq.wuye.entity.Result;
import com.sctlqq.wuye.entity.User;
import com.sctlqq.wuye.service.GoodsService;
import com.sctlqq.wuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sctlqq/api/wuye/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询所有 积分商品
     * @return
     */
    @GetMapping("/getgoodslist")
    public Result<Object> getGoodsList(){
        return new Result<>(HttpStatus.OK.value(),"success",goodsService.list());
    }

    /**
     * 查询所有 积分商品
     * @return
     */
    @PostMapping("/getlikegoodslist")
    public Result<Object> getLikeGoodsList(@RequestBody String key){
        return new Result<>(HttpStatus.OK.value(),"success",
                goodsService.list(new QueryWrapper<Goods>().like("name",key)
                        .or().like("price",key)));
    }

    /**
     * 通过给定对象 新增单个商品
     * @param goods
     * @return
     */
    @PutMapping("/addgoods")
    public Result<Object> addGoods(@RequestBody Goods goods){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",goodsService.save(goods));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 修改单个商品
     * @param goods
     * @return
     */
    @PostMapping("/updategoods")
    public Result<Object> updateGoods(@RequestBody Goods goods){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",goodsService.updateById(goods));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 删除单个或多个商品信息
     * @param goodsList
     * @return
     */
    @DeleteMapping("/deletegoods")
    public Result<Object> deleteNotice(@RequestBody List<Goods> goodsList){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",goodsService.removeBatchByIds(goodsList));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

}
